﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM.UserInterface
{
    public partial class cbProvince : UserControl
    {
        public string Prov_ID;
        public string Prov_Name;
        //──────────────────────────────────────────────────────────────────
        public cbProvince()
        {
            InitializeComponent();
            this.Load += new EventHandler(cbProvince_Load);
            cbProv.SelectedValueChanged += new EventHandler(cbProv_SelectedIndexChanged);
            cbProv.TextChanged += new EventHandler(cbProv_TextChanged);
        }
        //──────────────────────────────────────────────────────────────────
        void cbProv_TextChanged(object sender, EventArgs e)
        {
            if (this.cbProv.Text.Trim() == "") cbProv_SelectedIndexChanged(sender, e);
        }
        //──────────────────────────────────────────────────────────────────
        public void cbProvince_Load(object sender, EventArgs e)
        {
            try
            {
                //if (this.cbProv.DataSource == null)
                //{

                //    DataTable dt = new CMS_GYM.Address().dt_getProvince();
                //    if (dt == null) return;
                //    cbProv.DataSource = new DataView(dt);
                //    cbProv.DisplayMember = dt.Columns[1].ColumnName;
                //    cbProv.ValueMember = dt.Columns[0].ColumnName;
                //    this.SetProvince("16");
                //    cbProv_SelectedIndexChanged(sender, e);
                //}
            }
            catch (Exception) { }
        }

        public void LoadData()
        {
            try
            {
                if (this.cbProv.DataSource == null)
                {

                    DataTable dt = new CMS_GYM.Address().dt_getProvince();
                    if (dt == null) return;
                    cbProv.DataSource = new DataView(dt);
                    cbProv.DisplayMember = dt.Columns[1].ColumnName;
                    cbProv.ValueMember = dt.Columns[0].ColumnName;
                    this.SetProvince("8");
                    cbProv_SelectedIndexChanged(null, null);
                }
            }
            catch (Exception) { }
        }
        //──────────────────────────────────────────────────────────────────
      public  void cbProv_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged();            
        }
        //──────────────────────────────────────────────────────────────────
        private void SelectedIndexChanged()
        {
            if (cbProv.SelectedValue == null)
            {
                Prov_ID = "8";
                Prov_Name = "Hồ Chí Minh";
                return;
            }
            Prov_ID =cbProv.SelectedValue==null? string.Empty: cbProv.SelectedValue.ToString();
            Prov_Name = cbProv.Text;
        }
        //──────────────────────────────────────────────────────────────────
        public void SetProvince(string s_ProvID)
        {
            if (s_ProvID == "" || s_ProvID == null || s_ProvID == "NULL")
            {
                cbProv.SelectedValue = DBNull.Value;
                cbProv.Text = "";
            }
            else
            {
                cbProv.SelectedValue = s_ProvID;

            }
        }
        //──────────────────────────────────────────────────────────────────
        public void SetDataSource(DataTable dtProv)
        {
            cbProv.DataSource = dtProv;
            cbProv.DisplayMember = dtProv.Columns[1].ColumnName;
            cbProv.ValueMember = dtProv.Columns[0].ColumnName;
            SelectedIndexChanged();
            if (dtProv.Rows.Count == 0) this.cbProv.SelectedValue=string.Empty;
        }        
    }
}
